<?php 
class cls_data_customer extends cls_data_base
{
	var $table_name = '`customer`';
	
	function check_login($email, $md5pwd)
	{
		$table_name = '`customer`';
	
		$sql = "select * from {$table_name} where Email = '{$email}'";
		$res = $this->db->getRow($sql);
		if (empty($res))
		{
			return return_info(false, "邮箱不存在");
		}
		if ($res["Password"] != $md5pwd)
		{
			return return_info(false, "密码错误");
		}
		else if($res["Validated"] == 0)
		{
			return return_info(false, "账号未激活，请登录邮箱查收激活邮件");
		}
		else
		{
			return return_info(true, $res);
		}
	}
	
	function get_customer_information($customerid)
	{
		$table_name = '`customer`';
	
		$sql = "select Name, Email, Birthday, Location from {$table_name} where ID = {$customerid}";
		$customer_information = $this->db->getRow($sql);
		if (empty($customer_information))
		{
			return false;
		}
		else
		{
			return $customer_information;
		}
	}
	
	function get_customer_information_by_email($email)
	{
		$table_name = '`customer`';
	
		$sql = "select ID, Name, Email, Birthday, Location from {$table_name} where Email = '{$email}'";
		$customer_information = $this->db->getRow($sql);
		if (empty($customer_information))
		{
			return false;
		}
		else
		{
			return $customer_information;
		}
	}

	function register($name, $md5pwd, $email, $birthday, $location)
	{
		$table_name = '`customer`';
	
		$input = array(
				'Name' => $name,
				'Password' => $md5pwd,
				'Email' => $email,
				'Birthday' => $birthday,
				'Location' => $location,
				'Validated' => 0
		);
		$res = $this->db->insert($table_name, $input);
		return $res;
	}
	
	function check_email_exist($email)
	{
		$table_name = '`customer`';
		
		$sql = "select * from {$table_name} where Email = '{$email}'";
		$customer_information = $this->db->query($sql);
		$matched_record = $this->db->num_rows($customer_information);
		if($matched_record == 0)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	
	function update_customer_information($customerid, $name, $birthday, $location)
	{
		$table_name = '`customer`';
		
		$input = array(
				'Name' => $name,
				'Location' => $location
				);
		$result = $this->db->update($table_name, $input, "ID = {$customerid}", 1);
		return $result;
	}
	
	function update_customer_password($customerid, $old_md5pwd, $new_md5pwd)
	{
		$table_name = '`customer`';
		
		$sql = "select * from {$table_name} where ID = '{$customerid}'";
		$result = $this->db->getRow($sql);
		if ($result['Password'] == $old_md5pwd)
		{
			$input = array(
					"Password" => $new_md5pwd);
			$update_result = $this->db->update($table_name, $input, "ID = {$customerid}", 1);
			if ($update_result == 1)
			{
				return return_info(true);
			}
			else
			{
				return return_info(true);
			}
		}
		else
		{
			return return_info(false, "原始密码错误");
		}
	}
	
	function reset_password($customerid, $new_md5pwd)
	{
		$table_name = '`customer`';
		
		$input = array(
				"Password" => $new_md5pwd);
		$update_result = $this->db->update($table_name, $input, "ID = '{$customerid}'", 1);
		if($update_result == 1)
		{
			return return_info(true);
		}
		// else
		 {
		 	return return_info(false, "新密码与原密码一致，无需更新");
		 }
	}
	
	function check_reset_right($customerid, $verify_code)
	{
		$table_name = '`emailverify`';

		$sql = "select * from {$table_name} where CustomerID = {$customerid} and VerifyCode = '{$verify_code}'";
		$res = $this->db->getRow($sql);
		if(empty($res))
		{
			return return_info(false, "密码重置验证码错误");
		}
		else
		{
			return return_info(true);
		}
	}

	function delete_verify_info($customerid, $verify_code)
	{
		$table_name = '`emailverify`';

		$result = $this->db->delete($table_name, "CustomerID = {$customerid} and VerifyCode = '{$verify_code}'");
	}

	function add_email_verify($customerid, $verify_code)
	{
		$table_name = '`emailverify`';
		
		$input = array(
				'CustomerID' => $customerid,
				'VerifyCode' => $verify_code
				);
		
		$result = $this->db->insert($table_name, $input);
		return $result;
	}
	
	function add_pwd_reset_verify($customerid, $verify_code)
	{
		$table_name = '`emailverify`';

		$input = array(
				'CustomerID' => $customerid,
				'VerifyCode' => $verify_code
				);

		$result = $this->db->insert($table_name, $input);
		return $result;
	}

	function verify_email($customerid, $verify_code)
	{
		$verify_table_name = '`emailverify`';
		$customer_table_name = '`customer`';
		
		$sql = "select * from {$verify_table_name} where CustomerID = {$customerid} and VerifyCode = '{$verify_code}'";
		$res = $this->db->getRow($sql);
		if(empty($res))
		{
			return return_info(false, "激活验证码错误");
		}
		else
		{
			$input = array(
				'Validated' => 1
				);
			$result = $this->db->update($customer_table_name, $input, "ID = {$customerid}", 1);
			if($result == 1)
			{
				return return_info(true);
			}
			else
			{
				return return_info(false, "用户已激活或链接不正确");
			}
		}
	}
}
?>